<template>
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-1"></div>

      <div class="row col-md-10">
        <!--左列-->
        <div class="col-md-3">
          <div class="col-md-12">
            <UserSubjectPanel></UserSubjectPanel>
          </div>
          <div class="col-md-12">
            <UserArmsPanel></UserArmsPanel>
          </div>
        </div>

        <!-- 中列 -->
        <div class="col-md-6">
          <!--个人信息-->
          <div class="col-md-12">
            <UserinfoPanel :mainTimeHandle="mainTimeHandle" :isLevelUp="isLevelUp"></UserinfoPanel>
          </div>

          <!--个人经历-->
          <div class="col-md-12">
            <UserLogPanel :userLog="userLog"></UserLogPanel>
          </div>
        </div>

        <!-- 右列 -->
        <div class="col-md-3">
          <!--好友列表-->
          <div class="col-md-12">
            <UserMatePanel></UserMatePanel>
          </div>
          <!--物品列表-->
          <div class="col-md-12">
            <UserBoxPanel></UserBoxPanel>
          </div>
        </div>
      </div>

      <div class="col-md-1"></div>

      <UserAttrPanel></UserAttrPanel>

      <NotifiedMessage></NotifiedMessage>

    </div>
  </div>


</template>

<script>
import UserSubjectPanel from './components/UserSubject.vue'
import UserArmsPanel from './components/UserArms.vue'
import UserinfoPanel from './components/UserInfo.vue'
import UserLogPanel from './components/UserLog.vue'
import UserMatePanel from './components/UserMate.vue'
import UserBoxPanel from './components/UserBox.vue'
import UserAttrPanel from './components/UserAttr.vue'
import Base from "./store/modules/base"
import Subject from "./store/modules/subject"
import Mate from "./store/modules/mate"
import Arms from "./store/modules/arms"
import Log from "./store/modules/log"
import Lang from "./store/language/game"
import NotifiedMessage from "@/components/NotifiedMessage";

export default {
  name: 'App',
  components: {
    UserSubjectPanel,
    UserArmsPanel,
    UserinfoPanel,
    UserLogPanel,
    UserMatePanel,
    UserBoxPanel,
    UserAttrPanel,
    NotifiedMessage
  },
  data:function (){
    return {
      mainTimeHandle:1,
      isLevelUp:false,
    }
  },
  mounted() {
    // 初始属性
    this.initData();
    this.mainTimeHandle = setInterval(this.timeHandle, Base.timeSpeed);
    // this.__getSubject();
    // this.__getArms();
    // this.__getMate();
  },
  methods: {
    handleUpdate(value) {
      this.messageVisible = value
    },
    initData(){
      // 是否具备突破条件
      if(this.$store.state.user.userInfo.exp>=this.$store.state.user.userInfo.levelUpExp){
        this.isLevelUp= true
      }
    },

    // 时间处理器
    timeHandle() {
      this.$store.state.user.other.month++;
      // 持续恢复
      this.__healOverTime();
      if (this.$store.state.user.other.month > 12) {
        this.$store.state.user.other.year++; // 增加年份
        this.$store.state.user.other.month = 1; // 重置月份
        this.$store.state.user.userInfo.age++; // 年龄增加
        this.$store.state.user.userInfo.life--; // 寿元减少
        // 剩余寿元小于0
        this.$store.commit('user/updateMonth',this.$store.state.user.other.month);
      }

      // 检测寿元是否耗尽
      if (this.$store.state.user.userInfo.life <= 0) {
        clearInterval(this.mainTimeHandle);
        this.$message.openMessage('寿元耗尽死亡，游戏结束')
      }

      // 检测血量是否耗尽
      if (this.$store.state.user.userInfo.hp <= 0) {
        clearInterval(this.mainTimeHandle);
        this.$message.openMessage('受伤过重死亡，游戏结束')
      }

      // 1-9月改为01-09月
      if(this.$store.state.user.other.month<10){
        this.$store.state.user.other.month = '0'+this.$store.state.user.other.month
      }
      switch (this.$store.state.user.userInfo.state){
        case 1:this.getExpHandle()
          break;
        case 2:this.travelHandle()
          break;
        default:
          this.studyHandle()
      }

      this.$store.commit('user/updateMonth',this.$store.state.user.other.month);
    },

    // 修炼处理
    getExpHandle() {
      this.$store.state.user.userInfo.state = 1;
      let expSpeed = Math.ceil(this.$store.state.user.userInfo.expSpeed*(100+this.$store.state.user.userFlair.expSpeed)/100) + this.$store.state.user.userInfo.buff.expSpeed;
      this.$store.state.user.userInfo.exp = this.$store.state.user.userInfo.exp===null?0:this.$store.state.user.userInfo.exp;

      this.$store.state.user.userInfo.exp += expSpeed;
      if (this.$store.state.user.userInfo.exp >= this.$store.state.user.userInfo.levelUpExp) {
        // 修为已满
        this.$store.state.user.userInfo.exp = this.$store.state.user.userInfo.levelUpExp
        // 具备突破条件
        this.isLevelUp = true;
        this.$store.state.user.userInfo.state = 0;
        // this.$message.openMessage({title:'提示',content:'已到达突破瓶颈，突破后才可修炼'},this)
      }
      this.expProgress = this.$store.state.user.userInfo.exp / this.$store.state.user.userInfo.levelUpExp * 100;
      this.$store.commit('user/updateMonth', this.$store.state.user.other.month);
    },

    // 练功处理
    studyHandle() {
      // 是否有主修功法
      if(this.$store.state.user.userSubject.length<=0||this.$store.state.user.userSubject[0].state!==1){
        return false;
      }
      this.$store.state.user.userSubject[0].studyExp += this.$store.state.user.userFlair.studySpeed;
      // 当前对应熟练等级
      let masteryLevel = Subject.masteryLevel[this.$store.state.user.userSubject[0].masteryLevel];
      if(this.$store.state.user.userSubject[0].studyExp>=masteryLevel.limit){
        this.$store.state.user.userSubject[0].studyExp = 0;
        this.$store.state.user.userSubject[0].masteryLevel += 1;
        // 每升一级 增加一个基础buff
        this.$store.state.user.userInfo.buff.expSpeed += this.$store.state.user.userSubject[0].expSpeedBuff
      }

      this.$store.commit('user/updateMonth',this.$store.state.user.other.month);
    },

    // 历练处理
    travelHandle() {
      this.$store.state.user.userInfo.state = 2;
      let rand = Math.floor(Math.random() * 100);
      if (rand < 10) {
        this.__getSubject()
      } else if (rand < 20) {
        this.__getArms()
      } else if (rand < 25) {
        this.__getMate()
      } else if (rand < 40) {
        this.__getExp()
      }
    },

    /**
     * 获得功法
     * @private
     */
    __getSubject() {
      let userBoxSize = Base.getUserBoxSize(this.$store.state.user.userInfo);
      let userBoxUseSize =  Base.getUserBoxUseSize(this.$store.state.user.userBox);

      if (userBoxUseSize >= userBoxSize) {
        return false
      }
      let logs = null;
      let getSubject = Subject.generateSubject();

      // 判断品阶是否保留
      if(!this.$store.state.user.config.travel.subject.includes(getSubject.level)){
        return false
      }

      // 判断属性是否保留
      if(!Base.keepAttr(getSubject,this.$store.state.user.config.travel.attr)){
        return false
      }

      this.$store.state.user.userBox.subject.unshift(getSubject);
      let date = this.$store.state.user.other.year + Lang.userInfoPanel.year + this.$store.state.user.other.month + Lang.userInfoPanel.month;
      logs = Log.generateLiLianSubjectLog(date, getSubject.name)

      if (logs !== null) {
        this.$store.state.user.userLog.unshift(logs);
        this.$store.commit('user/updateMonth', this.$store.state.user.other.month);
      }
    },

    /**
     * 获得法宝
     * @private
     */
    __getArms() {
      let userBoxSize = Base.getUserBoxSize(this.$store.state.user.userInfo);
      let userBoxUseSize =  Base.getUserBoxUseSize(this.$store.state.user.userBox);
      if (userBoxUseSize >= userBoxSize) {
        return false
      }
      let logs = null;
      let getArms = Arms.generateArms();

      // 判断是否保留
      if(!this.$store.state.user.config.travel.arms.includes(getArms.level)){
        return false
      }

      // 判断属性是否保留
      if(!Base.keepAttr(getArms,this.$store.state.user.config.travel.attr)){
        return false
      }

      this.$store.state.user.userBox.arms.unshift(getArms);
      let date = this.$store.state.user.other.year + Lang.userInfoPanel.year + this.$store.state.user.other.month + Lang.userInfoPanel.month;
      logs = Log.generateLiLianArmsLog(date, getArms.name)

      if (logs !== null) {
        this.$store.state.user.userLog.unshift(logs);
        this.$store.commit('user/updateMonth', this.$store.state.user.other.month);
      }
    },

    /**
     * 结识道侣
     * @private
     */
    __getMate() {
      // 获取道侣列表容量
      let userMateSize = Base.getUserMateSize(this.$store.state.user.userInfo);
      let userMateUseSize = this.$store.state.user.userMate.length
      if (userMateUseSize >= userMateSize) {
        return false
      }

      // 生成新的道侣
      let mate = Mate.generateMate(this.$store.state.user.userInfo.level);
      // 判断是否保留
      if(!this.$store.state.user.config.travel.mate.includes(mate.level)){
        return false
      }

      let logs = null;
      // 暂存双修道侣
      let sexMate = this.$store.state.user.userMate[0]
      if (sexMate && sexMate.state === 1) {
        this.$store.state.user.userMate.splice(0, 1);
      }

      this.$store.state.user.userMate.unshift(mate)
      // 将双修道侣放回收尾
      if (sexMate && sexMate.state === 1) {
        this.$store.state.user.userMate.unshift(sexMate);
      }

      let date = this.$store.state.user.other.year + Lang.userInfoPanel.year + this.$store.state.user.other.month + Lang.userInfoPanel.month;
      logs = Log.generateLiLianMateLog(date, mate.name)

      if (logs !== null) {
        this.$store.state.user.userLog.unshift(logs);
        this.$store.commit('user/updateMonth', this.$store.state.user.other.month);
      }
    },

    /**
     * 获得修为
     * @private
     */
    __getExp() {
      // 随机获得总修为的0-10被的修为点
      let rand = Math.floor(Math.random() * 10);
      if (rand > 0) {
        let expSpeed = this.$store.state.user.userInfo.expSpeed + this.$store.state.user.userInfo.buff.expSpeed
        this.$store.state.user.userInfo.exp += expSpeed * rand;
        if(this.$store.state.user.userInfo.exp>this.$store.state.user.userInfo.levelUpExp){
          this.$store.state.user.userInfo.exp=this.$store.state.user.userInfo.levelUpExp;
          // 具备突破条件
          this.isLevelUp = true;
        }
        // 生成日志
        let date = this.$store.state.user.other.year + Lang.userInfoPanel.year + this.$store.state.user.other.month + Lang.userInfoPanel.month;
        let logs = Log.generateLiLianExp(date, expSpeed * rand)
        this.$store.state.user.userLog.unshift(logs);
        this.$store.commit('user/updateMonth', this.$store.state.user.other.month);
      }
    },

    /**
     * 持续恢复
     * @private
     */
    __healOverTime(){
      // 已死亡不恢复
      if(this.$store.state.user.userInfo.hp <= 0){
        return true
      }

      this.$store.state.user.userInfo.hp +=  (this.$store.state.user.userInfo.hot+this.$store.state.user.userInfo.buff.hot)
      if(this.$store.state.user.userInfo.hp > (this.$store.state.user.userInfo.hpTotal+this.$store.state.user.userInfo.buff.hp)){
        this.$store.state.user.userInfo.hp = this.$store.state.user.userInfo.hpTotal+this.$store.state.user.userInfo.buff.hp
      }
      this.$store.commit('user/updateMonth', this.$store.state.user.other.month);
    }
  },
}
</script>

<style lang="scss">
</style>
<style scoped>

/*@import "assets/css/primary-theme.css";*/

</style>
